import { post } from "../utils/Client";

/**缓存 */
class BaseStorage {
  private storageType: string;
  constructor(storageType) {
    this.storageType = storageType;
  }
  /**
   * @param {*} key 存储的key值，查找的时候可以通过key进行查找，如果key重复则为覆盖
   * @param {*} value 存储的值  如果是其他类型，需要转为字符串进行存储
   * @param {*} force 如果force为true,则覆盖写入，如果force为false则可能存在写入失败
   * @returns Promise<any>
   */
  setSync(key: string, value: string, force = true) {
    return post("messageStorage_write", { msg_type: this.storageType, key, value, force });
  }
  /**
   * 根据key获取缓存
   * @param {*} key key
   * @returns Promise<any>
   */
  getSync(key: string) {
    return post("messageStorage_read", { msg_type: this.storageType, read_type: "value", key });
  }
  /**
   * 获取所有缓存信息
   * @returns Promise<any>
   */
  getInfoSync() {
    return post("messageStorage_read", {
      msg_type: this.storageType,
      read_type: "values",
    });
  }
}
/**会话存储 */
class AppSessionStorage extends BaseStorage {
  constructor() {
    super("tempporaryStorage");
  }
}
/**本地存储 */
class AppLocalStorage extends BaseStorage {
  constructor() {
    super("localStorage");
  }
}
/**缓存静态方法 */
class AppStorage {
  static session: AppSessionStorage = new AppSessionStorage();
  static local: AppLocalStorage = new AppLocalStorage();
}

export { AppStorage, AppLocalStorage, AppSessionStorage, BaseStorage };
